package com.android.myapplication.view.media;

import android.app.Service;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;

import androidx.annotation.RequiresApi;

public class MediaService extends Service {

    private final String TAG = MediaService.class.getSimpleName();

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "绑定成功");
        return (IBinder) new PlayMusicBinder();
    }

    private MediaPlayer mediaPlayer;

    /**
     * 增强版Binder，扩展出播放音乐🎵行为
     */
    class PlayMusicBinder extends Binder implements IMusicePlay {

        public PlayMusicBinder() {
            mediaPlayer = new MediaPlayer();
        }


        /**
         * 播放音乐
         *
         * @param musicPath 音乐文件的路径
         */
        @Override
        public void playMusic(String musicPath, int type) {
            try {
                mediaPlayer.reset();

                switch (type){
                    case 0: {
                        int resid = Integer.parseInt(musicPath);
                        AssetFileDescriptor afd = MediaService.this.getResources().openRawResourceFd(resid);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            mediaPlayer.setDataSource(afd);
                        } else {
                            mediaPlayer = MediaPlayer.create(MediaService.this, resid);
                        }
                        break;
                    }
                    case 1:{
                        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        mediaPlayer.setDataSource(musicPath);
                        break;
                    }
                    case 2:{
                        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        mediaPlayer.setDataSource(musicPath);
                        break;
                    }

                }
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mediaPlayer.start();
                    }
                });
                mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                    @Override
                    public boolean onError(MediaPlayer mp, int what, int extra) {
                        Toast.makeText(MediaService.this, "播放错误", Toast.LENGTH_LONG).show();
                        Log.d("AAA", "播放错误");

                        return false;
                    }
                });
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        Toast.makeText(MediaService.this, "播放完成", Toast.LENGTH_LONG).show();
                        Log.d("AAA", "播放完成，可以播放下一首了");
                    }
                });
                mediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                    @Override
                    public void onSeekComplete(MediaPlayer mp) {

                    }
                });
                mediaPlayer.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        /**
         * 暂停播放
         */
        @Override
        public void pausedPlay() {
            mediaPlayer.pause();
        }

        /**
         * 继续播放
         */
        @Override
        public void continuePlay() {
            mediaPlayer.start();
        }

        /**
         * 停止播放
         */
        @Override
        public void stopPlay() {
            mediaPlayer.stop();
        }

        @Override
        public boolean isPlaying() {
            return mediaPlayer.isPlaying();
        }

        @Override
        public IBinder asBinder() {
            return this;
        }
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d(TAG, "解绑成功");

        // 为什么解绑服务了，音乐还在播放，应该MediaPlay内部是一个服务

        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.release(); // 释放硬件播放资源
        }
        return super.onUnbind(intent);
    }
}
